﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.AlbLab.GEP.Dao.Base;
using Com.AlbLab.GEP.Dao.Interface;
using Com.AlbLab.GEP.Domain.Entities;
using Oracle.DataAccess.Client;
using Com.AlbLab.GEP.Domain;
using System.Data;

namespace Com.AlbLab.GEP.Dao.Impl
{
    public class PraticaM02Dao : OracleDao<OracleSessionManager, ISessionModel>, IPraticaM02Dao
    {
        private string insert = "INSERT INTO GEP_DATIM02"
                        + "(DM02_COD_PRATICA, DM02_RAG_SOC, DM02_COGNOME, DM02_NOME, DM02_TOPONIMO, DM02_VIA, DM02_CIV, DM02_SCALA, DM02_PIANO, DM02_INT, DM02_CAP, DM02_ISTAT, DM02_COMUNE, DM02_PROV, DM02_COD_RECLAMO, DM02_RIF_RECLAMO, "
                        + "DM02_CODFIS, DM02_PIVA, DM02_DATI_TEC_RIC, DM02_RIF_QUESITI)"
                        + " VALUES(:DM02_COD_PRATICA, :DM02_RAG_SOC, :DM02_COGNOME, :DM02_NOME, :DM02_TOPONIMO, :DM02_VIA, :DM02_CIV, :DM02_SCALA, :DM02_PIANO, :DM02_INT, :DM02_CAP, :DM02_ISTAT, :DM02_COMUNE, :DM02_PROV, :DM02_COD_RECLAMO, "
                        + ":DM02_RIF_RECLAMO, :DM02_CODFIS, :DM02_PIVA, :DM02_DATI_TEC_RIC, :DM02_RIF_QUESITI)";
        private string selectById = "select * from GEP_DATIM02 WHERE DM02_COD_PRATICA = :DM02_COD_PRATICA";

        public PraticaM02Dao(OracleSessionManager daoContext)
            : base(daoContext)
        {
            if (!base.Context.Session_isActive())
            {
                base.Context.Session_Init();
                base.CurrentConnection.Open();
            }
        }

        private OracleParameter[] MappingParams(GEP_DATIM02 r, bool isInsert)
        {
            OracleParameter[] oparams;
            oparams = new OracleParameter[20];

            oparams[0] = new OracleParameter("DM02_COD_PRATICA", OracleDbType.Decimal, r.Id_dm02_cod_pratica, ParameterDirection.Input);
            oparams[1] = new OracleParameter("DM02_RAG_SOC", OracleDbType.Varchar2, r.Dm02_Rag_Soc, ParameterDirection.Input);
            oparams[2] = new OracleParameter("DM02_COGNOME", OracleDbType.Varchar2, r.Dm02_Cognome, ParameterDirection.Input);
            oparams[3] = new OracleParameter("DM02_NOME", OracleDbType.Varchar2, r.Dm02_Nome, ParameterDirection.Input);
            oparams[4] = new OracleParameter("DM02_TOPONIMO", OracleDbType.Varchar2, r.Dm02_Toponimo, ParameterDirection.Input);
            oparams[5] = new OracleParameter("DM02_VIA", OracleDbType.Varchar2, r.Dm02_Via, ParameterDirection.Input);
            oparams[6] = new OracleParameter("DM02_CIV", OracleDbType.Varchar2, r.Dm02_Civ, ParameterDirection.Input);
            oparams[7] = new OracleParameter("DM02_SCALA", OracleDbType.Varchar2, r.Dm02_Scala, ParameterDirection.Input);
            oparams[8] = new OracleParameter("DM02_PIANO", OracleDbType.Varchar2, r.Dm02_Piano, ParameterDirection.Input);
            oparams[9] = new OracleParameter("DM02_INT", OracleDbType.Varchar2, r.Dm02_Int, ParameterDirection.Input);
            oparams[10] = new OracleParameter("DM02_CAP", OracleDbType.Varchar2, r.Dm02_Cap, ParameterDirection.Input);
            oparams[11] = new OracleParameter("DM02_ISTAT", OracleDbType.Varchar2, r.Dm02_Istat, ParameterDirection.Input);
            oparams[12] = new OracleParameter("DM02_COMUNE", OracleDbType.Varchar2, r.Dm02_Comune, ParameterDirection.Input);
            oparams[13] = new OracleParameter("DM02_PROV", OracleDbType.Varchar2, r.Dm02_Prov, ParameterDirection.Input);
            oparams[14] = new OracleParameter("DM02_COD_RECLAMO", OracleDbType.Varchar2, r.Dm02_Cod_Reclamo, ParameterDirection.Input);
            oparams[15] = new OracleParameter("DM02_RIF_RECLAMO", OracleDbType.Varchar2, r.Dm02_Rif_Reclamo, ParameterDirection.Input);
            oparams[16] = new OracleParameter("DM02_CODFIS", OracleDbType.Varchar2, r.Dm02_Codfis, ParameterDirection.Input);
            oparams[17] = new OracleParameter("DM02_PIVA", OracleDbType.Varchar2, r.Dm02_Piva, ParameterDirection.Input);
            oparams[18] = new OracleParameter("DM02_DATI_TEC_RIC", OracleDbType.Varchar2, r.Dm02_Dati_Tec_Ric, ParameterDirection.Input);
            oparams[19] = new OracleParameter("DM02_RIF_QUESITI", OracleDbType.Varchar2, r.Dm02_Rif_Quesiti, ParameterDirection.Input);

            //if (isInsert)
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Output);
            //else
            //    oparams[29] = new OracleParameter("TES_COD_PRATICA", OracleDbType.Decimal, r.Id_Tes_Cod_Pratica, ParameterDirection.Input);
            return oparams;
        }

        #region IDao<GEP_DATIM02,decimal> Members

        public Dictionary<decimal, GEP_DATIM02> GetAll()
        {
            throw new NotImplementedException();
        }

        public GEP_DATIM02 GetById(decimal id)
        {
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(selectById, CurrentConnection);
            cmd.Parameters.Add("DM02_COD_PRATICA", id);
            GEP_DATIM02 ut = null;
            try
            {
                using (OracleDataReader r = cmd.ExecuteReader())
                {
                    if (r.HasRows)
                    {
                        r.Read();
                        ut = DaoOracleDbHelper.MapDatiM02(r);
                    }
                }
            }
            catch (Exception e0x)
            {
                throw new ManagedException("EX-D01003", e0x);
            }
            return ut;
        }

        public void Insert(GEP_DATIM02 entity)
        {
            using (OracleCommand ocmd = new OracleCommand())
            {
                try
                {
                    ocmd.CommandText = insert;
                    ocmd.Parameters.AddRange(MappingParams(entity, false));
                    ocmd.Connection = base.CurrentConnection;
                    ocmd.ExecuteNonQuery();
                    // entity.Id_Tes_Cod_Pratica = (decimal)ocmd.Parameters["pID"].Value;
                }
                catch (Exception e0x)
                {
                    throw new ManagedException("EX-PR004", e0x);
                }
            }
        }

        public void Update(GEP_DATIM02 entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(decimal id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (!base.Context.TransactionModeOn)
            {
                base.CurrentConnection.Close();
            }
        }

        #endregion
    }
}